<?php
namespace App\Http\Controllers\Admin;

use App\Models\Action;
use App\Models\ActionLog;

/**
 * 行为控制器
 */
class ActionController extends BaseController
{

    public function __construct(Action $action, ActionLog $actionLog)
    {
        parent::__construct();
        $this->action = $action;
        $this->actionLog = $actionLog;
    }


    /**
     * 用户行为列表
     */
    public function getIndex()
    {
        $where['status'] = ['>', -1];
        $rows = $this->action->_getPageList($where);
        $rows['data'] = int_to_string($rows['data']);
        return view('admin.action.index', compact('rows'));
    }

    /**
     * 新增行为
     */
    public function getAdd()
    {
        return view('admin.action.edit');
    }

    /**
     * 编辑行为
     */
    public function getEdit()
    {
        $id = request('id');
        if (!$id) {
            return $this->error('参数不能为空！');
        }
        $data = $this->action->_getOne(['id' => $id]);
        return view('admin.action.edit', compact('data'));
    }

    /**
     * 更新行为
     */
    public function postSave()
    {
        $res = $this->action->addOrUpdate();
        if (!$res) {
            return $this->error($this->action->getError());
        }
        return $this->success(request('id') ? '更新成功！' : '新增成功！');
    }


    /**
     * 行为日志列表
     */
    public function getLog()
    {
        $rows = $this->actionLog->getLogList();
        return view('admin.action.log', compact('rows'));
    }

    /**
     * 查看行为日志
     */
    public function getShowLog()
    {
        $id = request('id');
        if (!$id) {
            return $this->error('参数错误！');
        }
        $info = $this->actionLog->getLogById($id);
        return view('admin.action.show_log', compact('info'));
    }

    /**
     * 删除日志
     */
    public function anyRemove()
    {
        $res = $this->actionLog->remove();
        if (!$res) {
            return $this->error($this->action->getError());
        }
        return $this->success('删除成功！');
    }

    /**
     * 清空日志
     */
    public function getClear()
    {
        $res = $res = $this->actionLog->_del();
        if (!$res) {
            return $this->error('日志清空失败！');
        }
        return $this->success('日志清空成功！');
    }

}
